﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoNideBaking : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateForm() != "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ValidateForm() + "');", true);
            return;
        }
        if (btnSave.CommandArgument == "")
        {
            siconide_baking model = new siconide_baking();
            model.Baking_Date = txtBaking_Date.Text.Trim().toDateTimeWithoutError();
            model.Start_Time = txtStart_Time.Text.Trim().toDateTimeWithoutError();
            model.End_Time = txtEnd_Time.Text.Trim().toDateTimeWithoutError();
            model.Drying_temp = Convert.ToDecimal(txtDrying_temp.Text.Trim());
            model.Operator = ddlOperator.SelectedValue;
            model.Remark = txtRemark.Text.Trim();
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_siconide_baking bll = new BLL_siconide_baking();
            try
            {


                
                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            siconide_baking model = new siconide_baking();
            model.ID = Convert.ToInt32(btnSave.CommandArgument);
            model.Baking_Date = txtBaking_Date.Text.Trim().toDateTimeWithoutError();
            model.Start_Time = txtStart_Time.Text.Trim().toDateTimeWithoutError();
            
            model.End_Time = txtEnd_Time.Text.Trim().toDateTimeWithoutError();
            model.Drying_temp = Convert.ToDecimal(txtDrying_temp.Text.Trim());
            model.Operator = ddlOperator.SelectedValue;
            model.Remark = txtRemark.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_siconide_baking bll = new BLL_siconide_baking();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        siconide_baking model = new siconide_baking();
        model.ID = Convert.ToInt32(id); ;

        BLL_siconide_baking bll = new BLL_siconide_baking();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_siconide_baking bll = new BLL_siconide_baking();
                List<siconide_baking> list = bll.select(Convert.ToInt32(id));
                foreach (siconide_baking tbl in list)
                {
                    btnSave.CommandArgument = tbl.ID.ToString();
                    txtBaking_Date.Text = tbl.Baking_Date.ToString("yyyy-MM-dd");
                    txtStart_Time.Text = tbl.Start_Time.ToString("hh:mm:ss");
                    txtEnd_Time.Text = tbl.End_Time.ToString("hh:mm:ss");
                    txtDrying_temp.Text = tbl.Drying_temp.ToString();
                    ddlOperator.SelectedValue = tbl.Operator;
                    txtRemark.Text = tbl.Remark;
                }
            }
        }
    }



    protected void ddlOperator_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlOperator.Items.Insert(0, li);

    }

    #region Validate Form

    private string ValidateForm()
    {
        string str = "";
        try
        {
            // Check if Client is Selected 
            if (txtBaking_Date.Text == "")
            {
                return "Please enter Baking Date";
            }
            if (txtStart_Time.Text == "") return "Please enter Start Time";
            if (txtEnd_Time.Text == "") return "Please enter End Time";
            if (txtDrying_temp.Text == "") return "Please enter Drying Temp";

            if (ddlOperator.SelectedIndex == 0) return "Please select Operator 2";
            if (txtRemark.Text == "") return "Please enter Remark";

            return str;
        }
        catch (Exception)
        {

            return "Error in Validation";
        }
        finally
        {
            str = null;
        }
    }

    #endregion

}